﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using System.Drawing;

namespace WXYW
{
    public partial class RWGLManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialUserList();

                TaskService service = new TaskService();
                Task task = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }

                subjectTxtBox.Text = task.Subject;
                createrLabel.Text = task.User1.ContactPerson;
                createDateLbl.Text = task.CreateDate.Value.ToString("yyyy-MM-dd");
                shouldCompleteDateTextBox.Text = task.ShouldCompleteDate.Value.ToString("yyyy-MM-dd");
                taskContentTxtBox.Text = task.Content;
                userDropDownList.SelectedValue = task.User.UserID.ToString();
                completeTextBox1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                taskStatusDropDownList.SelectedValue = task.Status;
                if (!string.IsNullOrEmpty(task.TaskType))
                {
                    TaskTypeRadioButtonList.SelectedValue = task.TaskType;
                }

                resultTextBox.Text = task.Result;
                if (!string.IsNullOrEmpty(task.ResultType))
                {
                    resultTypeDDL.SelectedValue = task.ResultType;
                }
                if (!string.IsNullOrEmpty(task.ReasonType))
                {
                    questionTypeDDL.SelectedValue = task.ReasonType;
                }
                feedbackTextBox.Text = task.Feedback;
                feedbackDateLbl.Text = task.FeedbackDate.HasValue ? task.FeedbackDate.Value.ToString("yyyy-MM-dd") : "";
                if (!task.TaskFile.IsLoaded)
                {
                    task.TaskFile.Load();
                }
                if (task.TaskFile.Count() > 0)
                {
                    taskFileLnkBtn.ForeColor = Color.Gray;
                }
            }
        }

        private void InitialUserList()
        {
            UserService userservice = new UserService();
            userDropDownList.DataSource = userservice.GetWorkingPeople();
            userDropDownList.DataTextField = "ContactPerson";
            userDropDownList.DataValueField = "UserID";
            userDropDownList.DataBind();
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("RWGL.aspx");
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            TaskService service = new TaskService();            
            Task taskEntity = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
            taskEntity.CompleteDate = Convert.ToDateTime(completeTextBox1.Text);
            taskEntity.Result = resultTextBox.Text.Trim();
            taskEntity.ResultType = resultTypeDDL.SelectedValue;
            taskEntity.Status = taskStatusDropDownList.SelectedValue;
            taskEntity.TaskType = TaskTypeRadioButtonList.SelectedValue;
            taskEntity.Subject = subjectTxtBox.Text.Trim();
            taskEntity.Content = taskContentTxtBox.Text.Trim();
            taskEntity.ReasonType = questionTypeDDL.SelectedValue;
            taskEntity.ShouldCompleteDate = Convert.ToDateTime(shouldCompleteDateTextBox.Text);
            if (taskEntity.User.UserID.ToString() != userDropDownList.SelectedValue)
            {
                UserService userService = new UserService();
                taskEntity.User = userService.GetUserByID(Convert.ToInt32(userDropDownList.SelectedValue));
            }
            taskEntity.Feedback = feedbackTextBox.Text.Trim();
            if (!taskEntity.FeedbackDate.HasValue)
            {
                taskEntity.FeedbackDate = DateTime.Now;
            }
            service.Update(taskEntity);

            Response.Write(string.Format("<script language='JavaScript'> alert( '保存成功');location.href= 'RWGL.aspx' </script> "));
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<TaskFile> dds = new List<TaskFile>();
            dds.Add(new TaskFile());
            return dds;
        }

        protected void TaskFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/TaskFile/{0}_{1}", ((TaskFile)e.Row.DataItem).ID, ((TaskFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
            TaskFileGridView.DataBind();
        }

        protected void uploadTaskFileBtn_Click(object sender, EventArgs e)
        {
            if (TaskFileFileUpload.HasFile)
            {
                TaskService taskService = new TaskService();
                long fileID = taskService.AddTaskFile(TaskFileFileUpload.FileName, Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileFileUpload.SaveAs(Server.MapPath(string.Format("TaskFile/{0}_{1}", fileID, TaskFileFileUpload.FileName)));

                IQueryable<TaskFile> files = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileGridView.DataSource = files;
                TaskFileGridView.DataBind();

                TaskFileModalPopupExtender.Show();
            }
        }

        protected void taskFileLnkBtn_Click(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
            TaskFileGridView.DataBind();

            TaskFileModalPopupExtender.Show();
        }
    }
}
